package com.zhl.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Filters;

public class CRUD {

	public static void main(String[] args) {
		CRUD crud = new CRUD();
		crud.conn("test");
		/*crud.findAll();
		crud.insertOne();
		crud.findAll();
		crud.insertMany();
		crud.findAll();*/
	   // crud.findSome();
	    crud.findAll();
	    crud.delete();
	    crud.findAll();  
	    crud.deleteAll();
	    crud.findAll();  
	}
	MongoClient mongoClient;
	MongoDatabase db ;

	
	public void insertOne(){
		
		//插入文档  
        /** 
        * 1. 创建文档 org.bson.Document 参数为key-value的格式 
        * 2. 创建文档集合List<Document> 
        * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document) 
        * */
		MongoCollection<Document> collection = db.getCollection("test");
        Document document = new Document("title", "MongoDB").  
        append("description", "database").  
        append("likes", 100).  
        append("by", "Fly");  

        collection.insertOne(document);  
 
        System.out.println("新增单个文档成功");  
		
	}
	
	public void insertMany(){
		
		//插入文档  
        /** 
        * 1. 创建文档 org.bson.Document 参数为key-value的格式 
        * 2. 创建文档集合List<Document> 
        * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document) 
        * */
		MongoCollection<Document> collection = db.getCollection("test");
        Document document1 = new Document("title", "MongoDB1").  
        append("description", "database1").  
        append("likes", 1001).  
        append("by", "Fly1");  
        Document document2 = new Document("title", "MongoDB2").  
        append("description", "database2").  
        append("likes", 1002).  
        append("by", "Fly2"); 
        Document document3 = new Document("title", "MongoDB3").  
        append("description", "database3").  
        append("likes", 1003).  
        append("by", "Fly3"); 
        List<Document> documents = new ArrayList<Document>();  
        documents.add(document1);   documents.add(document2);  documents.add(document3); 
        collection.insertMany(documents);  
        System.out.println("批量文档新增成功");  
		
	}
	
	
	
	//获取所有文档
	public void findAll(){
 
		MongoCollection<Document> doc =    db.getCollection("test");
		System.out.println("mycolbbb表数量："+doc.count());
		FindIterable<Document> findIterable = doc.find(); 
		MongoCursor<Document> mongoCursor = findIterable.iterator();  
		while(mongoCursor.hasNext()){
				System.out.println(mongoCursor.next());
		}

	}
	

	
	//获得所有符合条件的文档
	public void findSome(){
		MongoCollection<Document> doc =    db.getCollection("test");
		FindIterable<Document> t = doc.find( new Document("likes",100));
		MongoCursor<Document> a = t.iterator();
	    while(a.hasNext()){
			System.out.println(a.next());
	    }
	    a.close();
	}
	
	//修改
	public void update(){
		MongoCollection<Document> doc =    db.getCollection("test");
		doc.updateMany(Filters.eq("likes", 100), new Document("$set",new Document("likes",200)));  
		
		System.out.println("update：");
	}
	
	//删除一条符合条件的文档
	public void delete(){
		//删除符合条件的第一个文档  
		MongoCollection<Document> doc =    db.getCollection("test");
		doc.deleteOne(Filters.eq("likes", 1001));  
       
	}
	
	//删除所有符合条件的文档
	public void deleteAll(){
		 //删除所有符合条件的文档  
		MongoCollection<Document> doc =    db.getCollection("test");
		doc.deleteMany (Filters.eq("likes", 200));  
	}
	
	
	public void conn(String DBName){
		 mongoClient = new MongoClient( "localhost" , 27017 );
		 db = mongoClient.getDatabase(DBName);//访问 “test” 数据库
	}
}
